package com.dkd.manage.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Dto.ChannelConfigDto;
import com.dkd.manage.domain.vo.ChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 售货机货道Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-06
 */
@Service
public class ChannelServiceImpl implements IChannelService 
{
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     * 
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id)
    {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     * 
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel)
    {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     * 
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel)
    {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     * 
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids)
    {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     * 
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id)
    {
        return channelMapper.deleteChannelById(id);
    }

    /**
     * 根据当前商品id集合统计货道数量
     * @param skuIds
     * @return
     */
    public int countChannelBySkuIds(Long[] skuIds){
        return channelMapper.countChannelBySkuIds(skuIds);
    }

    /**
     * 根据内部货道编号查询货道信息
     *
     * @param innerCode
     * @return
     */
    @Override
    public List<ChannelVo> selectChannelVoListByInnerCode(String innerCode) {
        return channelMapper.selectChannelVoListByInnerCode(innerCode);
    }

    /**
     * 设置货道关联商品
     * @param channelConfigDto
     * @return
     */
    @Override
    @Transactional
    public int setChannel(ChannelConfigDto channelConfigDto) {
        //1.dto转为跟数据相关相关的po
        List<Channel> channels = channelConfigDto.getChannelList().stream().map(channelSkuDto -> {
            //先看一下数据库是否有这个商品(售货机内部编号innerCode和货道id进行查询)
            Channel channel=channelMapper.getChannelInfo(channelSkuDto.getInnerCode(),channelSkuDto.getChannelCode());
            if(channel!=null){
                //更改对应商品
                channel.setSkuId(channelSkuDto.getSkuId());
                //货道更新时间
                channel.setCreateTime(DateUtils.getNowDate());
            }
            return channel;
        }).collect(Collectors.toList());
        //2. 批量修改货道
        return channelMapper.batchUpdateChannel(channels);
    }
}
